package com.wc.算法提高课.C第三章_图论.单源最短路的综合应用.最优贸易;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/10/11 13:51
 * @description https://www.acwing.com/problem/content/description/343/
 */
public class Main {
    /**
     * 思路；正跑一遍, 反跑一遍 + spfa
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 100010, M = 2000010, INF = 110;
    static int[] h = new int[N], rh = new int[N], e = new int[M], ne = new int[M], w = new int[N];
    static int[] dmax = new int[N], dmin = new int[N], q = new int[N];
    static boolean[] st = new boolean[N];
    static int n, m, idx = 1;

    public static void main(String[] args) {
        n = sc.nextInt();
        m = sc.nextInt();
        for (int i = 1; i <= n; i++) w[i] = sc.nextInt();
        while (m-- > 0) {
            int a = sc.nextInt(), b = sc.nextInt(), z = sc.nextInt();
            add(h, a, b);
            add(rh, b, a);
            if (z == 2) {
                add(h, b, a);
                add(rh, a, b);
            }
        }
        spfa(h, 1, dmin, true);
        spfa(rh, n, dmax, false);
        int res = 0;
        for (int i = 1; i <= n; i++) res = Math.max(res, dmax[i] - dmin[i]);
        out.println(res);
        out.flush();
    }

    static void add(int[] h, int a, int b) {
        e[idx] = b;
        ne[idx] = h[a];
        h[a] = idx++;
    }

    static void spfa(int[] h, int start, int[] dist, boolean isMin) {
        if (isMin) Arrays.fill(dist, 1, n + 1, INF);
        int hh = 0, tt = 1;
        dist[start] = w[start];
        q[0] = start;
        st[start] = true;
        while (hh != tt) {
            int t = q[hh++];
            if (hh == N) hh = 0;
            st[t] = false;
            for (int i = h[t]; i > 0; i = ne[i]) {
                int j = e[i];
                if (isMin && dist[j] > Math.min(dist[t], w[j]) || !isMin && dist[j] < Math.max(dist[t], w[j])) {
                    if (isMin) dist[j] = Math.min(dist[t], w[j]);
                    else dist[j] = Math.max(dist[t], w[j]);
                    if (!st[j]) {
                        q[tt++] = j;
                        st[j] = true;
                    }
                }
            }
        }
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
